package com.mbbb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mbbb.constant.MessageConstant;
import com.mbbb.pojo.Menu;
import com.mbbb.pojo.Setmeal;
import com.mbbb.pojo.TravelGroup;
import com.mbbb.service.SetmealService;
import com.mbbb.util.QiniuUtils;
import com.mbbb.vo.PageResult;
import com.mbbb.vo.QueryPageBean;
import com.mbbb.vo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.util.UUID;

@RestController
@RequestMapping("/Setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile imgFile){
        try {
            byte[] bytes = imgFile.getBytes();
            String originalFilename = imgFile.getOriginalFilename();

            //上传后的文件名不要和原始文件名一致，否则多人上传同名文件时，会被覆盖
            //QiniuUtils.upload2Qiniu(bytes,originalFilename);

            //自定义上传后的文件名，保证唯一
            String newFileName = UUID.randomUUID().toString()+"_originalFilename";
            QiniuUtils.upload2Qiniu(bytes,newFileName);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PostMapping("/add")
    public Result add(Integer[] travelgroupIds , @RequestBody Setmeal setmeal){
        try {
            setmealService.add(travelgroupIds,setmeal);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
}
